package com.wdd.atcrowdfunding.manager.controller;

import com.wdd.atcrowdfunding.bean.Role;
import com.wdd.atcrowdfunding.bean.User;
import com.wdd.atcrowdfunding.manager.service.UserRoleService;
import com.wdd.atcrowdfunding.manager.service.UserService;
import com.wdd.atcrowdfunding.util.AjaxResult;
import com.wdd.atcrowdfunding.util.Page;
import com.wdd.atcrowdfunding.util.StringUtil;
import com.wdd.atcrowdfunding.vo.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;



    /**
     * 跳转后台主页面 ,异步加载显示数据
     * @param pageno
     * @param pagesize
     * @param queryText
     * @return
     */
    @RequestMapping("/doIndex")
    @ResponseBody
    public AjaxResult doIndex(@RequestParam(value = "pageno",required = false,defaultValue = "1") Integer pageno,
                        @RequestParam(value = "pagesize",required = false,defaultValue = "10")Integer pagesize,
                        String queryText){

        AjaxResult ajaxResult = new AjaxResult();
        try{
            Map<String,Object> paramMap = new HashMap();
            paramMap.put("pageno",pageno);
            paramMap.put("pagesize",pagesize);
            if(StringUtil.isNotEmpty(queryText)){
                if (queryText.contains("%")){
                    queryText = queryText.replaceAll("%","\\\\%");
                }
                if (queryText.contains("_")){
                    queryText = queryText.replaceAll("_","\\\\_");
                }
                paramMap.put("queryText",queryText);
            }
            Page<User> page = userService.queryUserPage(paramMap);
            ajaxResult.setSuccess(true);
            ajaxResult.setPage(page);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("查询数据失败");
        }
        return ajaxResult;
    }

    /**
     * 添加管理员
     * @param user
     * @return
     */
    @RequestMapping("/doAdd")
    @ResponseBody
    public AjaxResult doAdd(User user){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            if(userService.querySelectUserByLoginacct(user.getLoginacct())){
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("登录账号名称已存在");
                return ajaxResult;
            }
            if(userService.querySelectUserByUsername(user.getUsername())){
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("管理名称已存在已存在");
                return ajaxResult;
            }
            if(userService.querySelectUserByEmail(user.getEmail())){
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("邮箱已存在");
                return ajaxResult;
            }
            int count = userService.saveUser(user);
            ajaxResult.setSuccess(count==1);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("管理员添加失败");
        }
        return ajaxResult;
    }

    /**
     * 更改管理员
     * @param user
     * @return
     */
    @RequestMapping("/doUpdate")
    @ResponseBody
    public AjaxResult doUpdate(User user){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            int count = userService.updateUser(user);
            ajaxResult.setSuccess(count==1);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("管理员更新失败");
        }
        return ajaxResult;
    }

    /**
     * 删除管理员
     * @param id
     * @return
     */
    @RequestMapping("/doDelete")
    @ResponseBody
    public AjaxResult doDetlete(Integer id){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            int count = userService.deleteUser(id);
            ajaxResult.setSuccess(count==1);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("管理员删除失败");
        }
        return ajaxResult;
    }

    /**
     * 批量删除管理员
     * @param data
     * @return
     */
    @RequestMapping("/doDeleteBatch")
    @ResponseBody
    public AjaxResult doDeleteBatch(Data data){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            int count = userService.deleteBatchUserByVO(data);
            ajaxResult.setSuccess(count==data.getDatas().size());
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("管理员删除失败");
        }
        return ajaxResult;
    }

    @RequestMapping("/index")
    private String index(){
        return "user/index";
    }

    @RequestMapping("/toAdd")
    private String toAdd(){
        return "user/add";
    }

    @RequestMapping("/toUpdate")
    private String toUpdate(Integer id,Map map){
        User user = userService.getUserById(id);
        map.put("user",user);
        return "user/update";
    }

    /**
     * 显示页面分配角色
     * @param id
     * @param map
     * @return
     */
    @RequestMapping("/assignRole")
    private String assignRole(Integer id ,Map map){
        //查询所有的角色
        List<Role> allRoleList = userService.queryAllRole();
        //查询管理员已拥有的角色的id
        List<Integer> roleIds = userService.queryRoleByUserid(id);


        List<Role> leftRoleList = new ArrayList<Role>(); //未分配角色
        List<Role> rightRoleList = new ArrayList<Role>(); //已分配角色

        for(Role role : allRoleList){
            if(roleIds.contains(role.getId())){
                //已分配的角色
                rightRoleList.add(role);
            }else{
                //未分配的角色
                leftRoleList.add(role);
            }
        }
        map.put("rightRoleList",rightRoleList);
        map.put("leftRoleList",leftRoleList);

        return "user/assignRole";
    }

    /**
     * 分配角色
     * @param userid
     * @param data
     * @return
     */
    @RequestMapping("/doAssignRole")
    @ResponseBody
    public AjaxResult doAssignRole(Integer userid,Data data){
        AjaxResult ajaxResult = new AjaxResult();
        if(data.getIds()==null){
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("请选择角色");
            return ajaxResult;
        }
        try{
            userRoleService.saveUserRoleRelationship(userid,data);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("分配角色失败");
        }
        return ajaxResult;
    }

    /**
     * 取消分配角色
     * @param userid
     * @param data
     * @return
     */
    @RequestMapping("/doUnAssignRole")
    @ResponseBody
    public AjaxResult doUnAssignRole(Integer userid,Data data){
        AjaxResult ajaxResult = new AjaxResult();
        if(data.getIds()==null){
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("请选择角色");
            return ajaxResult;
        }
        try{
            userRoleService.deleteUserRoleRelationship(userid,data);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("取消分配角色失败");
        }
        return ajaxResult;
    }


}
